﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net;
using System.IO;

namespace DistributedSystems
{
    public class Utils
    {
        //input :postcode
        //output : ACT..
        public static string GetState(int pc)
        {
            string postCode = pc.ToString("0000");
            if (!IsValidPostCodeFormat(pc))
                return "";

            switch (postCode[0])
            {
                case '5': return "SA";             // SA:  5000-5999
                case '3':
                case '8': return "VIC";            // VIC: 3000-3999, 8000-8999
                case '4':
                case '9': return "QLD";            // QLD: 4000-4999, 9000-9999
                case '6': return "WA";             // WA:  6000-6999
                case '7': return "TAS";            // TAS: 7000-7999
                case '1': return "NSW";            // NSW: 1000-1999

                case '0':
                    switch (postCode[1])
                    {
                        case '8':
                        case '9': return "NT";         // NT:  0800-0999
                        case '2': return "ACT";        // ACT: 0200-0299
                        default: return "";
                    }

                case '2':
                    int tmp = Int32.Parse(postCode);
                    if ((tmp >= 2600 && tmp <= 2618) || (tmp >= 2900 && tmp <= 2920))
                        return "ACT";                  // ACT: 2600-2618, 2900-2920
                    else if ((tmp >= 2000 && tmp <= 2599) || (tmp >= 2619 && tmp <= 2898)
                                                          || (tmp >= 2921 && tmp <= 2999))
                        return "NSW";                  // NSW: 2000-2599, 2619-2898, 2921-2999
                    else return "";

                default: return "";
            }
        }

        public static bool IsValidPostCodeFormat(int pc)
        {
            string postCode = pc.ToString("0000");
            int tmp;
            if (String.IsNullOrEmpty(postCode) || postCode.Length != 4 || !Int32.TryParse(postCode, out tmp))
                return false;
            return true;
        }
    }
}